﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SYMetro.DataObjects;
using SYMetro.ServiceContract.Membership;
using YiDa.Infrastructure;

namespace SYMetro.Application.Test
{
    [TestClass]
    public class UserServiceTest
    {
        MembershipConfigDataObject config = null;
        [TestInitialize()]
        public void Initialize()
        {
            config = new MembershipConfigDataObject();
            config.EnablePasswordRetrieval = false;
            config.EnablePasswordReset = true;
            config.RequiresQuestionAndAnswer = false;
            config.RequiresUniqueEmail = false;
            config.MaxInvalidPasswordAttempts = 5;
            config.PasswordAttemptWindow = 10;
            config.MinRequiredPasswordLength = 1;
            config.MinRequiredNonalphanumericCharacters = 0;
            config.PasswordStrengthRegularExpression = "";
            config.PasswordFormat = "Hashed";
        }

        [TestMethod]
        public void TestCreateUser()
        {
            var service = ServiceLocator.Instance.GetService<IUserService>();

            UserDataObjectList list = new UserDataObjectList();
            UserDataObject u = new UserDataObject()
            {
                ID = Guid.NewGuid().ToString(),
                UserName = "TestUser",
                Password = "123456",
                DisplayName = "测试用户",
                PasswordAnswer = "what's your name",
                PasswordQuestion = "Wudonglei",
                Email = "abc@123.com"
            };
            list.Add(u);

            var result = service.CreateUser(config, list);
            Assert.AreEqual(1, result.Count);

            Assert.IsTrue(service.DeleteUser(u.UserName));
        }

        [TestMethod]
        public void TestGetUserByEmailAndChangeQuestionAndAnswer()
        {
            var service = ServiceLocator.Instance.GetService<IUserService>();

            var u = service.GetUserNameByEmail("abc@123.com");

            Assert.IsNotNull(u);

            var nu = service.ChangePasswordQuestionAndAnswer(u.UserName, "123456", "how old are you", "31");

            Assert.IsTrue(nu);
        }

        [TestMethod]
        public void TestGetPassword()
        {
            var service = ServiceLocator.Instance.GetService<IUserService>();

            var nu = service.GetPassword("TestUser", "31");

            Assert.AreEqual("123456", nu);
        }

    }
}
